import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/flowing_water_iview.dart';
import 'package:flutter_deer/account/models/flowing_water_entity.dart';
import 'package:flutter_deer/account/presenter/flowing_water_presenter.dart';
import 'package:flutter_deer/account/provider/flowing_water_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import '../../order/page/order_page.dart';

class AccountRecordListPage extends StatefulWidget {

  const AccountRecordListPage({super.key});

  @override
  _AccountRecordListPageState createState() => _AccountRecordListPageState();
}

class _AccountRecordListPageState extends State<AccountRecordListPage> with BasePageMixin<AccountRecordListPage, FlowingWaterPagePresenter>, AutomaticKeepAliveClientMixin<AccountRecordListPage> implements FlowingWaterIMvpView {

  FlowingWaterProvider provider = FlowingWaterProvider();
  List<String> types = [];

  bool get wantKeepAlive => true;
  @override
  FlowingWaterPagePresenter createPresenter() => FlowingWaterPagePresenter();
  @override
  void setFlowingWaterList(List<FlowingWaterEntity>? flowingWaters){
    provider.setFlowingWaterList(flowingWaters);
  }

  @override
  Widget build(BuildContext context) {
    types = ['${DeerLocalizations.of(context)!.ftRegisterReward.nullSafe}','${DeerLocalizations.of(context)!.ftShareReward.nullSafe}','${DeerLocalizations.of(context)!.ftRecharge.nullSafe}','${DeerLocalizations.of(context)!.ftBetting.nullSafe}','${DeerLocalizations.of(context)!.ftCancelOrder.nullSafe}','${DeerLocalizations.of(context)!.ftWithdraw.nullSafe}','${DeerLocalizations.of(context)!.ftBettingWin.nullSafe}','${DeerLocalizations.of(context)!.ftRewardExpire.nullSafe}','${DeerLocalizations.of(context)!.ftFreeze.nullSafe}','${DeerLocalizations.of(context)!.ftUnfreeze.nullSafe}','${DeerLocalizations.of(context)!.platformBenefits.nullSafe}'];
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: '${DeerLocalizations.of(context)!.accountStatement.nullSafe}',
      ),
      body: ChangeNotifierProvider<FlowingWaterProvider>(
        create: (_) => provider,
        child: Consumer<FlowingWaterProvider>(
          builder: (_,provider,child){
            if(provider.stateType==StateType.loading){
              return StateLayout(type: StateType.loading);
            }
            return CustomScrollView(
              slivers: [
                for (int i = 0; i < provider.flowingWaterList!.length; i++)
                  _buildGroup(provider.flowingWaterList!.elementAt(i))
              ],
            );
          }
        )
      )
    );
  }

  Widget _buildGroup(FlowingWaterEntity? flowingWater) {
    return SliverMainAxisGroup(
      slivers: [
        SliverPersistentHeader(
          pinned: true,
          delegate: SliverAppBarDelegate(
            Container(
              alignment: Alignment.centerLeft,
              width: double.infinity,
              color: ThemeUtils.getStickyHeaderColor(context),
              padding: const EdgeInsets.only(left: 16.0),
              child: Text('${flowingWater!.date}'),
            )
            , 34.0,
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate((_, index) {
            return _buildItem(flowingWater.list!.elementAt(index),index);
          },
            childCount: flowingWater.list!.length,
          ),
        ),
      ],
    );
  }

  Widget _buildItem(FlowingWaterItemEntity? item,int i) {
    TextStyle textStyle = TextStyles.textBold14;
    String? scorePrefix = '';
    if(item!.type=='1'){
      textStyle = TextStyle(
        color: Colors.red,
        fontWeight: FontWeight.bold,
      );
      scorePrefix = '+';
    }else if(item!.type=='2'){
      textStyle = TextStyle(
        color: Colors.green,
        fontWeight: FontWeight.bold,
      );
      scorePrefix = '-';
    }else if(item.type=='3'){
      textStyle = TextStyle(
        color: Colors.amber,
        fontWeight: FontWeight.bold,
      );
    }
    String typeContent = types.elementAt(int.parse(item!.waterContentType.toString())-1);
    return Container(
      height: 72.0,
      width: double.infinity,
      padding: const EdgeInsets.all(15.0),
      decoration: BoxDecoration(
        border: Border(
          bottom: Divider.createBorderSide(context, width: 0.8),
        ),
      ),
      child: IndexedSemantics(
        index: i,
        child: Stack(
          children: <Widget>[
            Text(typeContent??''),
            Positioned(
              top: 0.0,
              right: 0.0,
              child: Text('${scorePrefix}${NumberFormat.decimalPattern('en_us').format(item.score)}',
                style: textStyle,
              ),
            ),
            Positioned(
              bottom: 0.0,
              left: 0.0,
              child: Text(item!.createTime??'', style: Theme.of(context).textTheme.titleSmall),
            ),
            Positioned(
              bottom: 0.0,
              right: 0.0,
              child: Text('${DeerLocalizations.of(context)!.balance.nullSafe}：${NumberFormat.decimalPattern('en_us').format(item.afterScore)}', style: Theme.of(context).textTheme.titleSmall),
            ),
          ],
        ),
      ),
    );
  }
}
